﻿Imports TransportadoraToledoDAL
Imports System.Data

Public Class EntregasBLL
    Dim objDAL As EntregasDAL

#Region "propriedades"
    Private lItensConsumo As New List(Of EntregasBLL)
    Public Property plItensConsumo() As List(Of EntregasBLL)
        Get
            Return lItensConsumo
        End Get
        Set(ByVal value As List(Of EntregasBLL))
            lItensConsumo = value
        End Set
    End Property

    Private codigo As Integer
    Public Property pCodigo() As Integer
        Get
            Return codigo
        End Get
        Set(ByVal value As Integer)
            codigo = value
        End Set
    End Property

    Private codCarga As Integer
    Public Property pcodCarga() As Integer
        Get
            Return codCarga
        End Get
        Set(ByVal value As Integer)
            codCarga = value
        End Set
    End Property

    Private lotacao As Integer
    Public Property pLotacao() As Integer
        Get
            Return lotacao
        End Get
        Set(ByVal value As Integer)
            lotacao = value
        End Set
    End Property

    Private dataEntrega As Date
    Public Property pDataEntrega() As Date
        Get
            Return dataEntrega
        End Get
        Set(ByVal value As Date)
            dataEntrega = value
        End Set
    End Property
    
    Private dataRetorno As Date
    Public Property pDataRetorno() As Date
        Get
            Return dataRetorno
        End Get
        Set(ByVal value As Date)
            dataRetorno = value
        End Set
    End Property

    Private almoco As Double
    Public Property pAlmoco() As Double
        Get
            Return almoco
        End Get
        Set(ByVal value As Double)
            almoco = value
        End Set
    End Property

    Private jantar As Double
    Public Property pJantar() As Double
        Get
            Return jantar
        End Get
        Set(ByVal value As Double)
            jantar = value
        End Set
    End Property

    Private pedagio As Double
    Public Property pPedagio() As Double
        Get
            Return pedagio
        End Get
        Set(ByVal value As Double)
            pedagio = value
        End Set
    End Property

    Private hotel As Double
    Public Property pHotel() As Double
        Get
            Return hotel
        End Get
        Set(ByVal value As Double)
            hotel = value
        End Set
    End Property

    Private combustivel As Double
    Public Property pCombustivel() As Double
        Get
            Return combustivel
        End Get
        Set(ByVal value As Double)
            combustivel = value
        End Set
    End Property


    Private descricaoOutros As String
    Public Property pDescricaoOutros() As String
        Get
            Return descricaoOutros
        End Get
        Set(ByVal value As String)
            descricaoOutros = value
        End Set
    End Property


    Private conserto As Double
    Public Property pConserto() As Double
        Get
            Return conserto
        End Get
        Set(ByVal value As Double)
            conserto = value
        End Set
    End Property

    Private outros As Double
    Public Property pOutros() As Double
        Get
            Return outros
        End Get
        Set(ByVal value As Double)
            outros = value
        End Set
    End Property

    Private subtotalMotorista As Double
    Public Property pSubTotalMotorista() As Double
        Get
            Return subtotalMotorista
        End Get
        Set(ByVal value As Double)
            subtotalMotorista = value
        End Set
    End Property

    Private subtotalVeiculo As Double
    Public Property pSubTotalVeiculo() As Double
        Get
            Return subtotalVeiculo
        End Get
        Set(ByVal value As Double)
            subtotalVeiculo = value
        End Set
    End Property

    Private status As String
    Public Property pStatus() As String
        Get
            Return status
        End Get
        Set(ByVal value As String)
            status = value
        End Set
    End Property

    Private statusLotacao As String
    Public Property pStatusLotacao() As String
        Get
            Return statusLotacao
        End Get
        Set(ByVal value As String)
            statusLotacao = value
        End Set
    End Property


    Private kmChegada As Integer
    Public Property pKmChegada() As Integer
        Get
            Return kmChegada
        End Get
        Set(ByVal value As Integer)
            kmChegada = value
        End Set
    End Property


    Private kmSaida As Integer
    Public Property pKmSaida() As Integer
        Get
            Return kmSaida
        End Get
        Set(ByVal value As Integer)
            kmSaida = value
        End Set
    End Property


    Private kmPercorrido As Integer
    Public Property pKmPercorrido() As Integer
        Get
            Return kmPercorrido
        End Get
        Set(ByVal value As Integer)
            kmPercorrido = value
        End Set
    End Property

    Private protocolo As String
    Public Property pProtocolo() As String
        Get
            Return protocolo
        End Get
        Set(ByVal value As String)
            protocolo = value
        End Set
    End Property

#End Region

#Region "Métodos"
    Sub New()

    End Sub

    Sub New(ByVal codigo As Integer, ByVal lotacao As Integer, ByVal dataEntrega As Date,
  ByVal dataRetorno As Date, ByVal status As String, ByVal almoco As Double, ByVal jantar As Double, ByVal diversos As Double, ByVal hotel As Double,
  ByVal descricaoDiverso As String, ByVal conserto As Double, ByVal combustivel As Double, ByVal kmsaida As Integer, ByVal kmchegada As Integer, ByVal kmpercorrido As Integer,
  ByVal pedagio As Double, ByVal subtotalmotorista As Double, ByVal subtotalveiculo As Double)
        Me.codigo = codigo
        Me.lotacao = lotacao
        Me.dataEntrega = dataEntrega
        Me.dataRetorno = dataRetorno
        Me.status = status
        Me.almoco = almoco
        Me.jantar = jantar
        Me.outros = diversos
        Me.descricaoOutros = descricaoDiverso
        Me.hotel = hotel
        Me.conserto = conserto
        Me.combustivel = combustivel
        Me.kmSaida = kmsaida
        Me.kmChegada = kmchegada
        Me.kmPercorrido = kmpercorrido
        Me.pedagio = pedagio
        Me.subtotalVeiculo = subtotalveiculo
        Me.subtotalMotorista = subtotalmotorista
        Me.pedagio = pedagio
        Me.outros = outros
        Me.status = status
    End Sub

    Sub inserir()
        Try
            
                'inserir
                objDAL = New EntregasDAL
            Me.codigo = objDAL.inserir(Me.lotacao, Me.dataEntrega, Me.dataRetorno, Me.status, Me.almoco, Me.jantar, Me.outros, Me.descricaoOutros,
                                       Me.hotel, Me.subtotalMotorista, Me.combustivel, Me.pedagio, Me.conserto, Me.kmSaida, Me.kmChegada, Me.kmPercorrido, Me.subtotalVeiculo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub


    'Localizar Lotação
    Function localizarLotacao(ByVal dtInicio As Date, ByVal dtFinal As Date) As DataTable
        Dim tab As DataTable
        Dim objDAL = New EntregasDAL
        Try
            tab = objDAL.localizarLotacao(dtInicio, dtFinal)
            objDAL = Nothing
            Return tab
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Function localizarEntrega(ByVal dtInicio As Date, ByVal dtFinal As Date) As DataTable
        Dim tab As DataTable
        Dim objDAL = New EntregasDAL
        Try
            tab = objDAL.localizarEntrega(dtInicio, dtFinal)
            objDAL = Nothing
            Return tab
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    'localizar uma linha da tabela w preenche as propriedades
    Sub localizar(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New EntregasDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)("ent_codigo").ToString
                Me.lotacao = tab.Rows(0)("lot_codigo").ToString
                Me.dataEntrega = tab.Rows(0)("ent_data_entrega").ToString
                Me.dataRetorno = tab.Rows(0)("ent_data_retorno").ToString
                Me.status = tab.Rows(0)("ent_status").ToString
                Me.almoco = tab.Rows(0)("ent_gasto_almoco").ToString
                Me.jantar = tab.Rows(0)("ent_gasto_jantar").ToString
                Me.outros = tab.Rows(0)("ent_gasto_diversos").ToString
                Me.descricaoOutros = tab.Rows(0)("ent_desc_diversos").ToString
                Me.hotel = tab.Rows(0)("ent_gasto_hotel").ToString
                Me.conserto = tab.Rows(0)("ent_conserto").ToString
                Me.combustivel = tab.Rows(0)("ent_despesa_combustivel").ToString
                Me.kmSaida = tab.Rows(0)("ent_km_saida").ToString
                Me.kmChegada = tab.Rows(0)("ent_km_chegada").ToString
                Me.kmPercorrido = tab.Rows(0)("ent_km_percorrido").ToString
                Me.pedagio = tab.Rows(0)("ent_despesa_pedagio").ToString
                Me.subtotalVeiculo = tab.Rows(0)("ent_subtotal_veiculo").ToString
                Me.subtotalMotorista = tab.Rows(0)("ent_subtotal_motorista").ToString

            Else
                limpar()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub limpar()
        Me.codigo = Nothing
        Me.lotacao = Nothing
        Me.status = Nothing
        Me.dataEntrega = Nothing
        Me.dataRetorno = Nothing
        Me.pedagio = Nothing
        Me.outros = Nothing
    End Sub

    Public Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New EntregasDAL
            tab = objDAL.localiza(codigo)
            If (tab.Rows.Count > 0) Then
                Me.lotacao = tab.Rows(0).Item("lot_codigo").ToString
                Me.statusLotacao = tab.Rows(0).Item("lot_status").ToString
                'carregar a lista de Itens Consumo
                localizarLotacao(codigo)
            Else
                limpar()
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub


    Public Sub atualizaEmTransito(ByVal codigo As Integer)

        Try
            objDAL = New EntregasDAL
            objDAL.atualizaEmTransito(codigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub

    Public Sub todosEntregue(ByVal lotacao As Integer, ByVal status As Integer)
        Try
            objDAL = New EntregasDAL
            objDAL.todosEntregue(lotacao, status)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub cargaEntregue(ByVal Status As Integer, ByVal carga As Integer)
        Try
            objDAL = New EntregasDAL
            objDAL.cargaEntregue(Status, carga)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub localizarLotacao(ByVal codigo As Integer)
        Dim objCons As New EntregasBLL
        Dim tab As DataTable
        tab = objCons.localizarLot(codigo)
        lItensConsumo.Clear()
        For Each linha As DataRow In tab.Rows
            objCons = New EntregasBLL
            objCons.codCarga = linha("car_codigo")
            objCons.protocolo = linha("car_protocolo")
            objCons.status = linha("car_status")
            lItensConsumo.Add(objCons)

        Next
    End Sub

    Public Function localizarLot(ByVal codigo As Integer) As DataTable
        Dim tab As DataTable
        Try
            objDAL = New EntregasDAL
            tab = objDAL.localiza(codigo)
        Catch ex As Exception
            Throw ex
        End Try
        Return tab
    End Function
#End Region
End Class
